package com.dreamcode.anfeedreader.activity;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.dreamcode.anfeedreader.collectors.FeedsDAOimpl;
import com.dreamcode.anfeedreader.modelClass.FeedItem;

public class SearchFeedItemsActivity extends Activity {
	
	private LayoutInflater layoutInflater;
	private FeedsDAOimpl feedItemsDAO;
	private ListView feedList = null;
	private Button btnFeeds = null;
	private Button btnItems = null;
	private Button btnAbout = null;
	private Button btnSearch = null;
	private List<FeedItem> searchResult = null;
	private EditText searchString = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        feedItemsDAO = FeedsDAOimpl.getInstance();
        setContentView(R.layout.searchfeeditems);
		initView();
    }
	private void initView() {
        layoutInflater = LayoutInflater.from(this);
        feedList = (ListView) findViewById(R.id.feedlist);
        searchResult = new LinkedList<FeedItem>();
        feedList.setAdapter(new StringListAdapter(searchResult));
    	btnAbout = (Button) findViewById(R.id.btnabout);
        btnAbout.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                AlertDialog groupDialog = new AlertDialog.Builder(SearchFeedItemsActivity.this).setTitle("AnFeedReader")
                .setMessage("Version : 0.1\nDevelopment:\nZarko Popovski\nLazar Pancevski")
                .setPositiveButton("OK", null).show();      
                
            }
        });
		searchString = (EditText) findViewById(R.id.txt);
		btnSearch = (Button) findViewById(R.id.search);		
		btnSearch.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if(searchString.getText().toString()!=null && !searchString.getText().toString().equals("")){
					searchResult.clear();
					searchResult = feedItemsDAO.search(SearchFeedItemsActivity.this, searchString.getText().toString());
					if(searchResult!=null&&searchResult.size()>0){
						//searchResult.addAll(searchResult);
						//stringListAdapter.notifyDataSetChanged();					    
					    feedList.setAdapter(new StringListAdapter(searchResult));
					    feedList.setOnItemClickListener(new OnItemClickListener(){

							@Override
							public void onItemClick(AdapterView<?> arg0,
									View arg1, int arg2, long arg3) {
								Intent i = new Intent(SearchFeedItemsActivity.this,FeedItemViewActivity.class);
					            i.putExtra("id", arg1.getId());
					            startActivity(i);
							}
					    });
					}else{
						Toast.makeText(SearchFeedItemsActivity.this, (CharSequence)Html.fromHtml(getString(R.string.no_result)), Toast.LENGTH_SHORT).show();
					}
				}else{
					Toast.makeText(SearchFeedItemsActivity.this, (CharSequence)Html.fromHtml(getString(R.string.nothing_to_search)), Toast.LENGTH_SHORT).show();
				}
			}
		});		
	
	}
		
    private class StringListAdapter extends BaseAdapter {
		private final List<FeedItem> feedItemList;

		public StringListAdapter(List<FeedItem> stringList) {
			this.feedItemList = stringList;
		}

		@Override
		public int getCount() {
			return feedItemList.size();
		}

		@Override
		public Object getItem(int position) {
			return feedItemList.get(position);
		}

		public FeedItem getItemObject(int position) {
			return feedItemList.get(position);
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		public int getIdFromMd5hashfromDB(String md5Hash) {
			return feedItemsDAO.getIdFromMd5Hash(SearchFeedItemsActivity.this,md5Hash);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
		    LinearLayout lView = new LinearLayout(SearchFeedItemsActivity.this);
		    lView.setGravity(Gravity.CENTER_VERTICAL);
		    lView.setId(getIdFromMd5hashfromDB(feedItemList.get(position).getMd5hash()));
			TextView textView = new TextView(SearchFeedItemsActivity.this);
			textView.setGravity(Gravity.CENTER_VERTICAL);
			textView.setTextSize(14);
			textView.setMinHeight(30);
			textView.setWidth(300);
			textView.setText((feedItemList.get(position)).getTitle());
			textView.setTextColor(Color.BLACK);
			lView.addView(textView);
			return lView;
		}
	}
}
